﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

using UncleBobWebService.Database;
using UncleBobWebService.Models;
using UncleBobWebService.ServiceRequester;


namespace UncleBobWebService
{
    public class UncleBobApplicantService : IWebService
    {
        //----------------------------------------------
        //----------------------------------------------


        //private static IDBAdapter dbAdapter = new ApplicantDBAdapter();
        private static string mConnectionString = ConfigurationManager.ConnectionStrings["AESDBConnectionString"].ConnectionString;
        //private static SqlConnection mConnection = new SqlConnection(mConnectionString);        
        //private static SqlCommand mCommand;

        // get rid of this
        public static Message mMsg = new Message();
            

        //***********************************************
        //***********************************************
        public Message RequestService(Message msg)
        {
            string action = msg.Category;
            Message thisMsg = new Message();
            try
            {
                switch (action)
                {                  
                    case MessageTypes.SERVICE_REGISTER_NEW_USER:
                        {
                            IDBAdapter dbAdapter = new ApplicantDBAdapter();
                            thisMsg = CreateData((RegisterModel)msg.Data);
                            break;
                        }

                    //case MessageTypes.SERVICE_SEARCH_SPECIFIC_DATA:
                    //    {
                    //        IDBAdapter dbAdapter = new ApplicantDBAdapter();
                    //        thisMsg = dbAdapter.SearchForSpecificData(msg, msg.Data);
                    //        break;
                    //    }
                    //case MessageTypes.SERVICE_SEARCH_ALL_DATA:
                    //    {
                    //        IDBAdapter dbAdapter = new ApplicantDBAdapter();
                    //        thisMsg = dbAdapter.SearchForAllData(msg);
                    //        break;
                    //    }
                    //case MessageTypes.SERVICE_EDIT_DATA:
                    //    {
                    //        IDBAdapter dbAdapter = new ApplicantDBAdapter();
                    //        thisMsg = dbAdapter.EditSpecificData(msg, msg.Data);
                    //        break;
                    //    }
                    //case MessageTypes.SERVICE_DELETE_SPECIFIC_DATA:
                    //    {
                    //        IDBAdapter dbAdapter = new ApplicantDBAdapter();
                    //        thisMsg = dbAdapter.DeleteSpecificData(msg, msg.Data);
                    //        break;
                    //    }
                    //case MessageTypes.SERVICE_DELETE_ALL_DATA:
                    //    {
                    //        IDBAdapter dbAdapter = new ApplicantDBAdapter();
                    //        thisMsg = dbAdapter.DeleteAllData(msg);
                    //        break;
                    //    }
                    case MessageTypes.SERVICE_DELETE_REGISTERED_USER:
                        {
                            IDBAdapter db = new RegisterDBAdapter();
                            thisMsg = db.DeleteSpecificData(msg, msg.Data);
                            break;
                        }
                    case MessageTypes.SERVICE_GET_JOB_QUESTIONS:
                        {
                            IDBAdapter db = new QuestionDBAdapter();                           
                            thisMsg = db.SearchForAllData(msg);
                            break;
                        }
                    case MessageTypes.SERVICE_CREATE_QUESTIONS:
                        {
                            IDBAdapter db = new QuestionDBAdapter();
                            thisMsg = db.CreateData(msg, msg.Data);
                            break;
                        }
                    case MessageTypes.SERVICE_DELETE_JOB_QUESTION:
                        {
                            IDBAdapter db = new QuestionDBAdapter();
                            thisMsg = db.DeleteSpecificData(msg, msg.Data);
                            break;
                        }

                    case MessageTypes.SERVICE_DELETE_ALL_JOB_QUESTIONS:
                        {
                            IDBAdapter db = new QuestionDBAdapter();
                            thisMsg = db.DeleteAllData(msg);
                            break;
                        }

                    case MessageTypes.SERVICE_GET_REGISTERED_USER:
                        {                            

                            IDBAdapter dbAdapter = new RegisterDBAdapter();
                            thisMsg = dbAdapter.SearchForAllData(msg);

                            RegisterModel tempModel = ((RegisterModel)thisMsg.Data).Clone();
                            Message tempMessage = new Message();

                            dbAdapter = new PermissionsDBAdapter();
                            tempMessage = dbAdapter.SearchForAllData(msg);
                            
                            // Copy the correct permissions over now.
                            tempModel.Permissions = ((RegisterModel)tempMessage.Data).Permissions;
                            thisMsg.Data = tempModel;                            
                            break;
                        }

                    case MessageTypes.SERVICE_ADD_NEW_JOB:
                        {
                            IDBAdapter dbAdapter = new StaffDBAdapter();
                            thisMsg = dbAdapter.CreateData(msg, msg.Data);
                            break;
                        }

                    case MessageTypes.SERVICE_GET_JOB_BY_TITLE:
                        {
                            IDBAdapter dbAdapter = new StaffDBAdapter();
                            thisMsg = dbAdapter.SearchForSpecificData(msg, msg.Data);
                            break;
                        }

                    case MessageTypes.SERVICE_DELETE_JOB_BY_TITLE:
                        {
                            StaffDBAdapter dbAdapter = new StaffDBAdapter();
                            thisMsg = dbAdapter.DeleteSpecificData(msg, msg.Data);
                            break;
                        }
                    case MessageTypes.SERVICE_VALIDATE_USER:
                        {
                            IDBAdapter dbAdapter = new RegisterDBAdapter();
                            thisMsg = dbAdapter.SearchForSpecificData(msg, msg.Data);
                            break;
                        }
                    case MessageTypes.SERVICE_GET_JOB_APPLICATIONS:
                        {
                            thisMsg.Result = false;
                            break;
                        }
                    case MessageTypes.SERVICE_GET_KIOSK_USER:
                        {
                            thisMsg.Result = false;
                            break;
                        }
                    case MessageTypes.SERVICE_SET_USER_AS_STAFFING_EXPERT:
                        {
                            IDBAdapter dbAdapter = new PermissionsDBAdapter();
                            thisMsg = dbAdapter.CreateData(msg, msg.Data);
                            break;
                        }

                    case MessageTypes.SERVICE_REMOVE_USER_PERMISSIONS:
                        {
                            IDBAdapter dbAdapter = new PermissionsDBAdapter();
                            thisMsg = dbAdapter.DeleteSpecificData(msg, msg.Data);
                            break;
                        }

                }
            }
            catch (Exception ex)
            {
                thisMsg.Data = string.Format("Exception {0} thrown", ex.Message);
            }

            msg = thisMsg;
            return msg;
        }


        public Message GetData()
        {
            return mMsg;

        }


        //**************************************************
        //Create an entry
        //**************************************************
        private Message CreateData(RegisterModel applicant)
        {
            Message thisMsg = new Message();
            SqlCommand mCommand = new SqlCommand();
            SqlConnection mConnection = new SqlConnection(mConnectionString);
            mCommand = mConnection.CreateCommand();
            mCommand.CommandText = string.Format("INSERT INTO UserRegistration (UserName, Password, FirstName, MI, LastName, Address, PhoneNumber) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}')"
                    , applicant.Username, applicant.Password, applicant.FirstName, applicant.MI, applicant.LastName, applicant.Address, applicant.PhoneNumber);

            try
            {
                mConnection.Open();
                mCommand.ExecuteNonQuery();
                mConnection.Close();
                thisMsg.Result = true;
            }
            catch (Exception error)
            {
                thisMsg.Result = false;
                thisMsg.Data = new String(error.Message.ToCharArray());
            }

            return thisMsg;
        }


        //**************************************************
        //Search for entry
        //**************************************************
        private Message SearchData(RegisterModel user)
        {
            Message thisMsg = new Message();
            thisMsg.Result = false;
            IDBAdapter db = new RegisterDBAdapter();
            try
            {
                if (db.SearchForSpecificData(thisMsg, user).Result)
                {
                    thisMsg.Data = user;
                    RegisterModel dbUser = (RegisterModel)db.SearchForAllData(thisMsg).Data;
                    if (dbUser.FirstName == null)
                    {                
                        thisMsg.Data = new String(string.Format("Account does not exist.\nUsername/Password miss match").ToCharArray());
                    }
                    else
                    {
                        thisMsg.Data = dbUser;
                        thisMsg.Result = true;
                    }
                }
            }
            catch (Exception error)
            {   
                thisMsg.Data = new String(error.Message.ToCharArray());
            }
        
            return thisMsg;
        } 
    }
}
